<?php
/**
 ***********************************************************************************************************************
 * 快递员客户管理
 ***********************************************************************************************************************
 */
namespace soa\account;
use soa\client;

/**
 * Class consumer
 */
class consumer extends client
{
    /**
     * 服务地址
     * @var string
     */
    public $service_url = 'http://account.core.kuaidihelp.com/';

    /**
     * 服务名称
     * @var string
     */
    protected $service_name = 'kuaidihelp_account';

    /**
     * @var int 加长超时时间
     */
    protected $_timeout = 20;


    /**
     * 添加单个客户资料
     * @param $courier_id
     * @param array $consumer
     * @return array|bool
     */
    public function add($courier_id, array $consumer)
    {
        if(
            empty($courier_id) ||
            !is_numeric($courier_id) ||
            empty($consumer)
        ) {
            return false;
        }
        return $this->post('v1/courier/consumer/add', ['courier_id' => $courier_id, 'consumer' => urlencode(json_encode($consumer))]);
    }

    /**
     * 批量添加添加客户
     * @param $courier_id
     * @param array $consumer
     * @return array|bool
     */
    public function batchAdd($courier_id, array $consumers)
    {
        if(
            empty($courier_id) ||
            !is_numeric($courier_id) ||
            empty($consumers)
        ) {
            return false;
        }
        return $this->post('v1/courier/consumer/batchAdd', ['courier_id' => $courier_id, 'consumers' => urlencode(json_encode($consumers))]);
    }


    /**
     * 更新一条客户资料
     * @param $courier_id
     * @param $consumer_id
     * @param array $consumer
     * @return bool
     */
    public function update($courier_id, $consumer_id, array $consumer)
    {
        if(
            empty($courier_id) ||
            !is_numeric($courier_id) ||
            empty($consumer_id) ||
            !is_numeric($consumer_id) ||
            empty($consumer) ||
            !is_array($consumer)
        ) {
            return false;
        }
        return $this->post('v1/courier/consumer/update', ['courier_id' => $courier_id, 'consumer_id' => $consumer_id, 'consumer' => urlencode(json_encode($consumer))]);
    }

    /**
     * @param $courier_id
     * @param $phone
     * @param array $consumer
     * @return bool
     */
    public function updateByPhone($courier_id, $phone, array $consumer)
    {
        if(
            empty($courier_id) ||
            !is_numeric($courier_id) ||
            empty($phone) ||
            empty($consumer) ||
            !is_array($consumer)
        ) {
            return false;
        }
        return $this->post('v1/courier/consumer/updateByPhone', ['courier_id' => $courier_id, 'consumer_phone' => $phone, 'consumer' => urlencode(json_encode($consumer))]);
    }

    /**
     * 批量更新客户资料
     * @param $courier_id
     * @param array $consumers  接受二维的多个consumer_id => consumer_data数组
     * @return bool
     */
    public function batchUpdate($courier_id, array $consumers)
    {
        if(
            empty($courier_id) ||
            !is_numeric($courier_id) ||
            empty($consumers) ||
            !is_array($consumers)
        ) {
            return false;
        }
        return $this->post('v1/courier/consumer/batchUpdate', ['courier_id' => $courier_id, 'consumers' => urlencode(json_encode($consumers))]);
    }


    /**
     * 删除一条客户资料
     * @param $courier_id
     * @param $consumer_id
     * @return bool
     */
    public function del($courier_id, $consumer_id)
    {
        if(
            empty($courier_id) ||
            !is_numeric($courier_id) ||
            empty($consumer_id) ||
            !is_numeric($consumer_id)
        ) {
            return false;
        }
        return $this->get('v1/courier/consumer/del', ['courier_id' => $courier_id, 'consumer_id' => $consumer_id]);
    }

    /**
     * 根据多个consumer_id批量删除客户资料
     * @param $courier_id
     * @param string $consumer_ids 多个ID使用逗号隔开
     * @return bool
     */
    public function delConsumerByIds($courier_id, $consumer_ids)
    {
        if(
            empty($courier_id) ||
            !is_numeric($courier_id) ||
            empty($consumer_ids) ||
            !is_string($consumer_ids)
        ) {
            return false;
        }
        return $this->get('v1/courier/consumer/delConsumerByIds', ['courier_id' => $courier_id, 'consumer_ids' => $consumer_ids]);
    }

    /**
     * 根据多个consumer_id批量删除客户资料
     * @param $courier_id
     * @param string $consumer_ids 多个ID使用逗号隔开
     * @return bool
     */
    public function delConsumerByPhones($courier_id, $phones)
    {
        if(
            empty($courier_id) ||
            !is_numeric($courier_id) ||
            empty($phones) ||
            !is_string($phones)
        ) {
            return false;
        }
        return $this->get('v1/courier/consumer/delConsumerByPhones', ['courier_id' => $courier_id, 'phones' => $phones]);
    }

    /**
     * 获取单条客户资料
     * @param $courier_id
     * @param $consumer_id
     * @return bool
     */
    public function getConsumer($courier_id, $consumer_id)
    {
        return $this->post('v1/courier/consumer/get', ['courier_id' => $courier_id, 'consumer_id' => $consumer_id]);
    }

    /**
     * 获取单条客户资料
     * @param $courier_id
     * @param $phone
     * @return bool
     */
    public function getConsumerByPhone($courier_id, $phone)
    {
        return $this->post('v1/courier/consumer/getByPhone', ['courier_id' => $courier_id, 'consumer_phone' => $phone]);
    }

    /**
     * 获取单条被删除的客户资料
     * @param $courier_id
     * @param $consumer_id
     * @return bool
     */
    public function getDeletedConsumer($courier_id, $consumer_id)
    {
        return $this->post('v1/courier/consumer/getDeleted', ['courier_id' => $courier_id, 'consumer_id' => $consumer_id]);
    }

    /**
     * 批量获取客户资料
     * @param int $courier_id 快递员ID 必须
     * @param array $condition 检索条件 支持检索条件 time_start(包括) time_end(不包括) sort_field(time\id) sort_by(asc\desc)
     * @param int $page 检索页数
     * @param int $page_num 每页显示数据条数 传0会返回全部数据
     * @param string $order_by
     * @return bool
     */
    public function getConsumers($courier_id, array $condition = [], $page = 1, $page_num = 500)
    {
        if(
            empty($courier_id) ||
            !is_numeric($courier_id)
        ) {
            return false;
        }
        // urlencode转义特殊字符
        foreach($condition as $key => $val) {
            if(!is_string($val) && !is_numeric($val)) {
                return false;
            }
            $condition[$key] = urlencode($val);
        }

        return $this->post(
            'v1/courier/consumer/batchGet',
            array_merge(
                $condition,
                [
                    'courier_id' => $courier_id,
                    'page' => $page,
                    'page_num' => $page_num,
                ]
            )
        );
    }

    /**
     * 批量获取被删除的客户资料
     * @param int $courier_id 快递员ID 必须
     * @param array $condition 检索条件 支持检索条件 time_start(包括) time_end(不包括) sort_field(time) sort_by(asc\desc)
     * @param int $page 检索页数
     * @param int $page_num 每页显示数据条数 传0会返回全部数据
     * @param string $order_by
     * @return bool
     */
    public function getDeletedConsumers($courier_id, array $condition = [], $page = 1, $page_num = 500)
    {
        if(
            empty($courier_id) ||
            !is_numeric($courier_id)
        ) {
            return false;
        }

        // urlencode转义特殊字符
        foreach($condition as $key => $val) {
            if(!is_string($val) && !is_numeric($val)) {
                return false;
            }
            $condition[$key] = urlencode($val);
        }

        return $this->post(
            'v1/courier/consumer/batchGetDeleted',
            array_merge(
                $condition,
                [
                    'courier_id' => $courier_id,
                    'page' => $page,
                    'page_num' => $page_num,
                ]
            )
        );
    }

}